﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UCWebpart.Admin
{
    public partial class EventManageControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //发邮件,申请被删除
            Worker w = new Worker();
            string id = (this.GridView1.DataKeys[e.RowIndex].Value).ToString();
           
            var mtr = new meetingroomManagementEntities();
            var cls = (from bm in mtr.MTR_bas_BookingManage join bs in mtr.MTR_bas_BookingSchedul 
                       on bm.bookingID equals bs.planId
                       where bm.bookingID == id 
                       select bm).SingleOrDefault<MTR_bas_BookingManage>();
            var clm = (from bm in mtr.MTR_bas_BookingManage
                       join bs in mtr.MTR_bas_BookingSchedul
                           on bm.bookingID equals bs.planId
                       where bm.bookingID == id
                       select bs).SingleOrDefault<MTR_bas_BookingSchedul>();
            var cmtr = (from bm in mtr.MTR_bas_BookingManage
                        join venue in mtr.MTR_bas_Meetingroom
                        on bm.Meetingroom equals venue.mtrId
                        where bm.bookingID == id
                        select venue).SingleOrDefault<MTR_bas_Meetingroom>();
            DateTime st = DateTime.Parse(clm.meetingStartDate.Value.ToLongDateString() + clm.startTime);
            DateTime et = DateTime.Parse(clm.meetingEndDate.Value.ToLongDateString() + clm.endTime);
            string mtrAdd = cmtr.mtrAddress + cmtr.mtrAddress1;
            string meetingTheme = cls.meetingTheme;
            string meetingAbstract = cls.meetingAbstract;
            string userName = cls.bookingPerson;
            string participate = cls.participants;

            string[] attendees = participate.Split(';');
            string[] cc = new string[0] ;
            //发送取消邮件
            //已经预定成功并且发出会议通知的
            if (GridView1.Rows[e.RowIndex].Cells[7].Text.ToString() == "2") {
                string des = "管理员: "+Session["userName"].ToString()+" 取消了本次会议";
                w.SendMeetingRequest(st, et, mtrAdd, meetingTheme, des, attendees, cc,
                              id, true, userName, userName + "@kaifa.cn", false, "");
            }
            if (GridView1.Rows[e.RowIndex].Cells[7].Text.ToString() == "1") { //待审批的，发邮件给申请人
                string des = "管理员: "+Session["userName"].ToString()+" 取消了您的主题为"+cls.meetingTheme+"的活动申请";
                Worker.sendEmailMessage(cls.bookingPerson+"@kaifa.cn","活动取消通知", Session["userName"].ToString() + "@kaifa.cn", des);
                //从审批流中取消此次审批
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //判断是否数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //找到GridView控件的第12个单元格中的 按钮 并给他添加 按钮事件 点击删除时提示 是否删除
                ((LinkButton)e.Row.Cells[0].Controls[0]).Attributes.Add("onclick",
                    string.Format("javascript:return confirm('您是要删除" + e.Row.Cells[3].Text + "主题为\"" + e.Row.Cells[6].Text + "\"的预定请求吗？')"));
            }
        }
    }
}